/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.wonderly.ham.types;

import java.io.Serializable;

/**
 *  Provides a wrapped RST value so that it can be used in GUI tables
 * and other places where comparing and sorting might be appropriate.
 * @author gregg
 */
public class RST implements Serializable, Comparable<RST> {
	/**
	 *  Stored values are shorts.
	 */
	protected short r, s, t;
	private static final long serialVersionUID = 1L;

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final RST other = (RST) obj;
		if (this.r != other.r) {
			return false;
		}
		if (this.s != other.s) {
			return false;
		}
		return this.t == other.t;
	}

	@Override
	public int hashCode() {
		int hash = 7;
		hash = 59 * hash + this.r;
		hash = 59 * hash + this.s;
		hash = 59 * hash + this.t;
		return hash;
	}
	/**
	 * Construct instance with shorts
	 * @param r
	 * @param s
	 * @param t
	 */
	public RST( short r, short s, short t ) {
		this.r = r;
		this.s = s;
		this.t = t;
	}
	/**
	 * Construct instance with integers
	 * @param r
	 * @param s
	 * @param t
	 */
	public RST( int r, int s, int t ) {
		this.r = (short)r;
		this.s = (short)s;
		this.t = (short)t;
	}
	/**
	 * Construct RST with T set to S.
	 * @param r
	 * @param s
	 */
	public RST( int r, int s ) {
		this.r = (short)r;
		this.s = (short)s;
		this.t = (short)s;
	}

	public RST( String rst ) {
		String arr[] = rst.split("/");
		if( arr.length >= 2 ) {
			r = Short.parseShort( arr[0] );
			t = s = Short.parseShort( arr[1] );
			if( arr.length > 2 ) {
				t = Short.parseShort( arr[2] );
			}
		}
	}

	/**
	 * Get just the R and S valus as r/s.
	 * @return the r and s values as a string r/s.
	 */
	public String getRS() {
		return r+"/"+s;
	}
	/**
	 * Get the R, S and T values in a string formatted
	 * as r/s/t.
	 * @return  the RST value as r/s/t
	 */
	public String getRST() {
		return r+"/"+s+"/"+t;
	}

	/**
	 * @return the r
	 */
	public short getR() {
		return r;
	}

	/**
	 * @param r the r to set
	 */
	public void setR(short r) {
		this.r = r;
	}

	/**
	 * @return the s
	 */
	public short getS() {
		return s;
	}

	/**
	 * @param s the s to set
	 */
	public void setS(short s) {
		this.s = s;
	}

	/**
	 * @return the t
	 */
	public short getT() {
		return t;
	}

	/**
	 * @param t the t to set
	 */
	public void setT(short t) {
		this.t = t;
	}

	public int compareTo(RST o) {
		if( r == o.r ) {
			if( s == o.s )
				return o.t - t;
			else
				return o.s - s;
		}
		return o.r - r;
	}
}
